package tools

import (
	"log"

	"gorm.io/gorm"
)

type File struct {
	gorm.Model
	FileIdentifier string `gorm:"primaryKey;unique;not null"`
	Content        []byte `gorm:"type:blob"`
}

func SaveFileToDB(db *gorm.DB, fileIdentifier string, content []byte) error {
	// 创建文件记录
	file := File{
		FileIdentifier: fileIdentifier,
		Content:        content,
	}

	// 存入数据库
	result := db.Create(&file)
	if result.Error != nil {
		log.Printf("Error saving file to DB: %v\n", result.Error)
		return result.Error
	}

	log.Println("File saved to DB successfully.")
	return nil
}
